import Taro from '@tarojs/taro-rn';
import * as tslib_1 from "tslib";
import React from 'react';
import { Component } from "@tarojs/taro-rn";
import { View, Image, Text } from "@tarojs/components-rn";
import { inject, observer } from "@tarojs/mobx-rn";
import { toJS } from "mobx";
import { getRuntime, numPartOf } from "../../utils/common";
import addressStyleSheet from "./address_styles";

import none_address from "../../assets/img/none_address.png";
import select_icon_on from "../../assets/img/icon_64_on.png";
import select_icon from "../../assets/img/icon_64.png";
var _styleSheet = addressStyleSheet;
let Company = class Company extends Component {
  constructor(props) {
    super(props);
    this.state = {
      address: [],
      currentAddressId: null
    };
  }
  handelRouter(url = '/pages/index/index') {
    getRuntime().navigateTo(url);
  }
  componentDidShow() {
    Taro.setNavigationBarTitle({ title: '收货地址' });
    this.getUserAddresses();
  }
  getUserAddresses() {
    let { userStore } = this.props.stores;
    let currentAddressId;
    userStore.getUserAddresses({
      callback: res => {
        let resArr = toJS(res.items);
        console.log(resArr);
        // 缓存地址信息用于编辑、订单选择地址
        resArr.map(item => {
          if (item.isDefault) {
            currentAddressId = item.id;
          }
        });
        this.setState({
          currentAddressId,
          address: resArr
        });
        getRuntime().setStorage('userAddress', resArr);
      }
    });
  }
  // 用户选择地址，缓存用户选择地址用于下单
  changeCurrentAddressId(item) {
    this.setState({
      currentAddressId: item.id
    });
    // getRuntime().setStorage('userSelectAddress', item)
    // 返回下单页
    //getRuntime().navigateBack()
  }
  render() {
    let { address, currentAddressId } = this.state;
    return <View>
        {address.length > 0 && <View style={_styleSheet["address-list"]}>
          {address.map(item => {
          return <View key={item.id} style={_styleSheet["item-address"]}>
                  <View onClick={this.changeCurrentAddressId.bind(this, item)}>
                    {item.id === currentAddressId && <Image src={select_icon_on} style={_styleSheet["select-icon"]} />}
                    {item.id !== currentAddressId && <Image src={select_icon} style={_styleSheet["select-icon"]} />}
                  </View>
                  <View style={_styleSheet["info"]}>
                    <View style={_styleSheet["line"]}>
                      <View style={_styleSheet["name"]}>{item.receiver}</View>
                      <View style={_styleSheet["tel"]}>{numPartOf(item.receiverPhone)}</View>
                    </View>
                    <View style={_styleSheet["line"]}>
                      <View style={_styleSheet["desc"]}>{item.receiverAddress}</View>
                      <View onClick={this.handelRouter.bind(this, '/pages/user/address_edit?id=' + item.id)} style={_styleSheet["btn-edit"]}>编辑</View>
                    </View>
                  </View>
                </View>;
        })}
        </View>}
        {address.length <= 0 && <View style={_styleSheet["my-card_none"]}>
            <View style={_styleSheet["none-info"]}>
                <Image src={none_address} style={_styleSheet["none-icon"]} />
                <Text style={[_styleSheet["none-title"], _styleSheet["list-desc"]]}>还没有添加收货地址哦~</Text>
            </View>
        </View>}
        <View onClick={this.handelRouter.bind(this, '/pages/user/address_add')} style={_styleSheet["page-submit"]}>+ 添加收货地址</View>
      </View>;
  }
};
Company = tslib_1.__decorate([inject("stores"), observer], Company);
export default Company;